package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.PhysicalExaminationDao;
import com.example.child_protection.dao.PhysicalExaminationValues2Dao;
import com.example.child_protection.dao.PhysicalExaminationValuesDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.PhysicalExaminationInsertRequestDTO;
import com.example.child_protection.dto.request.add.PhysicalExaminationValues2SaveRequestDTO;
import com.example.child_protection.dto.request.add.PhysicalExaminationValuesSaveRequestDTO;
import com.example.child_protection.dto.request.update.PhysicalExaminationUpdateRequestDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.ChildArchivesValuesModel;
import com.example.child_protection.model.PhysicalExaminationModel;
import com.example.child_protection.model.PhysicalExaminationValues2Model;
import com.example.child_protection.model.PhysicalExaminationValuesModel;
import com.example.child_protection.service.PhysicalExaminationManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 儿童档案管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class PhysicalExaminationManageServiceImpl extends MyBaseService implements PhysicalExaminationManageService {

    @Autowired
    public PhysicalExaminationDao physicalExaminationDao;

    @Autowired
    public PhysicalExaminationValuesDao physicalExaminationValuesDao;

    @Autowired
    public PhysicalExaminationValues2Dao physicalExaminationValues2Dao;

    /**
     * 添加
     *
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insert(PhysicalExaminationInsertRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.DEPARTMENT_INSERT, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<PhysicalExaminationInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(PhysicalExaminationInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                PhysicalExaminationModel model = new PhysicalExaminationModel();
                BeanUtils.copyProperties(requestDTO, model);
                model.setLastUpdatePersonId(model.getCreatePersonId());
                physicalExaminationDao.insert(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 保存体格检查信息
     *
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO saveValues(PhysicalExaminationValuesSaveRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.DEPARTMENT_INSERT, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<PhysicalExaminationValuesSaveRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(PhysicalExaminationValuesSaveRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                PhysicalExaminationModel model = new PhysicalExaminationModel();
                //如果没有体检记录数据则先添加体检记录
                if (null == requestDTO.getPeId()) {
                    model.setCreatePersonId(requestDTO.getCreatePersonId());
                    model.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    model.setArchivesId(requestDTO.getArchivesId());
                    model.setDate(requestDTO.getDate());
                    physicalExaminationDao.insert(model);
                } else {
                    //修改体检记录
                    model.setId(requestDTO.getPeId());
                    model.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    model.setDate(requestDTO.getDate());
                    physicalExaminationDao.updateSelective(model);
                    //删除已有的数据
                    PhysicalExaminationValuesModel dModel = new PhysicalExaminationValuesModel();
                    dModel.setPeId(requestDTO.getPeId());
                    dModel.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    physicalExaminationValuesDao.deleteByPeId(dModel);
                }

                //保存体检记录数据
                List<PhysicalExaminationValuesModel> infoValues = JSONObject.parseArray(requestDTO.getValues(), PhysicalExaminationValuesModel.class);
                for (PhysicalExaminationValuesModel c : infoValues) {
                    c.setArchivesId(requestDTO.getArchivesId());
                    c.setPeId(model.getId());
                    c.setCreatePersonId(requestDTO.getCreatePersonId());
                    c.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    physicalExaminationValuesDao.insert(c);
                }
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 保存专题数据信息
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO saveValues2(PhysicalExaminationValues2SaveRequestDTO requestDTO){
        return this.service(ServiceActionEnums.DEPARTMENT_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<PhysicalExaminationValues2SaveRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(PhysicalExaminationValues2SaveRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                PhysicalExaminationModel model = new PhysicalExaminationModel();
                //如果没有体检记录数据则先添加体检记录
                if(null == requestDTO.getPeId()){
                    model.setCreatePersonId(requestDTO.getCreatePersonId());
                    model.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    model.setArchivesId(requestDTO.getArchivesId());
                    physicalExaminationDao.insert(model);
                }else{
                    model.setId(requestDTO.getPeId());
                    //删除已有的数据
                    PhysicalExaminationValues2Model dModel = new PhysicalExaminationValues2Model();
                    dModel.setPeId(requestDTO.getPeId());
                    dModel.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    dModel.setCategoryId(requestDTO.getCategoryId());
                    physicalExaminationValues2Dao.deleteByPeIdAndCategoryId(dModel);
                }

                //保存体检记录数据
                List<PhysicalExaminationValues2Model> infoValues = JSONObject.parseArray(requestDTO.getValues(), PhysicalExaminationValues2Model.class);
                for(PhysicalExaminationValues2Model c :infoValues){
                    c.setArchivesId(requestDTO.getArchivesId());
                    c.setPeId(model.getId());
                    c.setCreatePersonId(requestDTO.getCreatePersonId());
                    c.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    physicalExaminationValues2Dao.insert(c);
                }

                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 修改
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO update(PhysicalExaminationUpdateRequestDTO requestDTO){
        return this.service(ServiceActionEnums.DEPARTMENT_UPDATE,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<PhysicalExaminationUpdateRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(PhysicalExaminationUpdateRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                PhysicalExaminationModel model = new PhysicalExaminationModel();
                BeanUtils.copyProperties(requestDTO,model);
                model.setLastUpdatePersonId(model.getCreatePersonId());
                physicalExaminationDao.updateSelective(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service (ServiceActionEnums.DEPARTMENT_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                PhysicalExaminationModel model = new PhysicalExaminationModel();
                BeanUtils.copyProperties(requestDTO,model);
                physicalExaminationDao.deleteForLogic(model);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

}
